<template>
	<view>
		<view class="page-body">
			<view class="page-section">
				<view class="page-section-gap">
					<view>
						<u-avatar :text="userinfo.realName.substr(userinfo.realName.length-1 ,1)" mode="circle" bg-color="#0071C5" color="#FFFFFF" size="100"></u-avatar>
					</view>
					<view class="content">
						<text class="u_name">{{userinfo.realName== null ? '': userinfo.realName }}</text>
						<text class="u_detail">{{userinfo.departmentName == null ? '': userinfo.departmentName }} - {{userinfo.positionName == null ? '': userinfo.positionName }}</text>
					</view>
				</view>
				<view class="page-section-content">
					<view class="page-section-content-date">
						{{this.dateTitle}}
					</view>
					<view class="page-section-content-time">
						{{this.timeTitle}}
					</view>
					<view class="page-section-content-lay">
						<view
							:class="clockStatus===0||clockStatus===1?'clock-btn':clockStatus===2?'clock-btn warning':'clock-btn err'"
							@click="pushchHandle">
							<text class="tip-text">{{this.btnTitle}}</text>
						</view>
					</view>
					<view class="page-section-content-range">
						<label class="iconfont icon-ym-yibanshixiang size"></label><text
							class="rang-title">您已进入打卡范围</text>
					</view>
				</view>
			</view>

			<view class="page-section-title">
				<view class="color-3A3A3A font-size30">今日打卡记录</view>
			</view>
			<view class="page-section-list" v-if="timeLine.length>0">
				<u-time-line class="timeLine">
					<u-time-line-item nodeTop="15" v-for="(item,tiemIndex) in timeLine" :key="tiemIndex">
						<template v-slot:node>
							<view class="time-circle" v-show="item.status===1 ? false : true"></view>
							<view class="time-circle-color" v-show="item.status===1 ? true : false">
								<view></view>
							</view>
						</template>
						<template v-slot:content>
							<view>
								<text v-if="item.status===1" class="time-title-black">
									<text>{{`${item.actualTime}：`}}</text>
								</text>
								<text v-if="item.status===0" class="time-title-gray">
									<text v-if="item.type===0">上班打卡：</text>
									<text v-else>下班打卡：</text>
								</text>
								<text class="time-content">
									{{item.shouldTime}}
								</text>
							</view>
						</template>
					</u-time-line-item>
				</u-time-line>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userinfo: {
					headIcon: undefined,
					realName: undefined,
					departmentName: undefined,
					positionName: undefined
				},
				timer: null,
				dateTitle: undefined,
				timeTitle: undefined,
				btnTitle: '上班打卡',
				clockStatus: 0,
				timeLine: []
			}
		},
		onLoad() {
			try {
				const userProvider = this.$store.state.vuex_user.userInfo

				if (userProvider) {
					this.userinfo = userProvider
					this.userinfo.headIcon = this.$u.http.config.baseUrl + userProvider.headIcon;
				}
			} catch (e) {}
			this.initClock()
		},
		onShow() {
			this.initData()
		},
		mounted() {

		},
		methods: {
			initClock() {
				clearInterval(this.timer)
				this.dateTitle = this.$u.timeFormat(new Date(), 'yyyy年mm月dd日')
				this.timeTitle = this.$u.timeFormat(new Date(), 'hh:MM:ss')

				this.timer = setInterval(() => {
					this.dateTitle = this.$u.timeFormat(new Date(), 'yyyy年mm月dd日')
					this.timeTitle = this.$u.timeFormat(new Date(), 'hh:MM:ss')
				}, 1000)
			},
			initData() {
				this.$u.api.clockInUserInfo().then(res => {
					this.clockStatus = res.cardStatus
					if (res.cardStatus === 0) {
						this.btnTitle = '上班打卡'
					} else if (res.cardStatus === 1) {
						this.btnTitle = '下班打卡'
					} else if (res.cardStatus === 2) {
						this.btnTitle = '更新打卡'
					} else {
						this.btnTitle = '禁止打卡'
					}
					this.timeLine = res.records
				}).catch(err => {
					console.error(err)
				})
			},
			pushchHandle() {
				let _data = {
					cardCoordinate: '',
					equipmentInformation: '',
					cardPlace: ''
				}
				this.$u.api.clockIn(_data).then(res => {
					let _url = 'onduty'
					if (res.type === 1) {
						_url = 'offduty'
					}
					this.setTime()
					this.$u.route(`pages/work/apply/fieldpunchcard/${_url}`, {
						opinion: JSON.stringify(res)
					})
				}).catch(err => {
					console.error(err)
				})
			},
			setTime() {
				if (this.clockStatus === 0) {
					uni.setStorageSync("goWorkTimeRecodrd", {
						time: this.timeTitle,
						date: new Date().getTime()
					})
					console.log(uni.getStorageSync("goWorkTimeRecodrd"))
				}
				if (this.clockStatus === 1) {
					uni.setStorageSync("offWorkTimeRecodrd", {
						time: this.timeTitle,
						date: new Date().getTime()
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page-section {
		height: 400px;
		padding: 10px 0 10px 0;
		background: #eeeeee;

		.page-section-gap {
			height: 80px;
			padding: 15px 20px;
			width: 100%;
			background: #ffffff;
			border-bottom: 1px #ececec solid;
			display: flex;

			.content {
				color: #333333;
				padding: 5px 0 0 15px;
				.u_name {
					width: 100%;
					float: left;
					font-size: 16px;
				}

				.u_detail {
					width: 100%;
					font-size: 12px;
					float: left;
				}
			}

		}

		.page-section-content {
			width: 100%;
			height: 300px;
			background: #ffffff;
			text-align: center;
			padding-top: 35px;
			font-weight: bold;

			.page-section-content-date {
				line-height: 12px;
				font-size: 12px;
			}

			.page-section-content-time {
				margin-top: 10px;
				line-height: 30px;
				font-size: 30px;

			}

			.page-section-content-lay {
				margin: 30px 0 15px 0;
				background-color: #FFFFFF;

				.clock-btn {
					margin: 0 auto;
					width: 120px;
					height: 120px;
					border-radius: 50%;
					box-shadow: -2px 2px 2px 1px #B8E9F8;
					background: #2196f3;
					text-align: center;
					line-height: 120px;
					color: #FFFFFF;
				}

				.clock-btn.err {
					box-shadow: -2px 2px 2px 1px #eeeeee;
					background: #cccccc;
				}

				.clock-btn.warning {
					box-shadow: -2px 2px 2px 1px #FBCB86;
					background: #FEA41C
				}

				.tip-text {

					font-size: 14px;
					text-align: center;
				}

				.tip-text.err {
					color: #ff0000;
				}
			}

			.page-section-content-range {
				color: #21AFFC;

				.size {
					font-size: 14px;
					margin-right: 5px;
				}

				.rang-title {
					font-size: 14px;
				}
			}
		}

	}

	.page-section-title {
		padding-left: 20px;
		height: 50px;
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		background: #ffffff;
		border-bottom: 1px #ececec solid;
		font-weight: bold;
	}

	.page-section-list {
		padding: 20px 30px;
		width: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		background: #ffffff;
		font-weight: bold;

		/* timeLIne样式 */
		.time-circle {
			display: flex;
			align-items: center;
			border-radius: 50%;
			border: 1px solid #0b91ea;
			width: 12px;
			height: 12px;
		}

		.time-circle-color {
			width: 12px;
			height: 12px;
			border-radius: 50%;
			border: 1px solid #0b91ea;
			display: flex;
			align-items: center;
			justify-content: center;

			view {
				width: 80%;
				height: 80%;
				border-radius: 50%;
				background: #0b91ea;
			}
		}

		.time-title-black {
			font-weight: bold;
			font-size: 14px;
			line-height: 14px;
			color: #000000;
		}

		.time-title-gray {
			font-weight: bold;
			font-size: 14px;
			line-height: 14px;
			color: #BFBFBF;
		}

		.time-content {
			font-size: 12px;
			line-height: 12px;
			color: #BFBFBF;
		}

		.timeLine::before {
			border-left: 2px solid #0b91ea;
			margin-top: 5px;
			height: 40px;
		}

	}
</style>
